package com.dcbeida.activity.stockdetail;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.JgmmxAdapter;
import com.dcbeida.adapter.SocketDetailAdapter;
import com.dcbeida.bean.StockDetail;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by 150410 on 2017/11/15.
 */

public class JgmMxActivity extends BaseActivity implements XListView.IXListViewListener, StockDetailContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_bh)
    TextView tvBh;
    @Bind(R.id.tv_yxrq)
    TextView tvYxrq;
    @Bind(R.id.tv_syts)
    TextView tvSyts;
    @Bind(R.id.tv_box)
    TextView tvBox;
    @Bind(R.id.myList)
    XListView myList;
    private StockDetail.ListBean bean;
    private JgmmxAdapter adapter;

    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 20;
    int type = DataLoadDirection.Refresh;
    private String isall = "1";//0 只显示在库存中的数据 1所有的记录
    private ArrayList<Object> datalist;
    private StockDetailPersentr mPresent;

    @Override
    protected int getLayoutResId() {
        return R.layout.jgmnx_activity;
    }

    @Override
    public void setData() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            bean = (StockDetail.ListBean) bundle.getSerializable("StockDetail.ListBean");
        }
        tvTitle.setText(R.string.jgmmx);
        tvBh.setText(bean.getDrugname());
        tvYxrq.setText(mContext.getString(R.string.ypph_yxq, bean.getBatchnumber(), bean.getExpiredate()));
        tvBox.setText(bean.getQuantity() + "");

        if ("1".equals(bean.getIsalarm())) {
            tvSyts.setText(bean.getExpiredays());
            tvSyts.setVisibility(View.VISIBLE);
            tvYxrq.setTextColor(mContext.getResources().getColor(R.color.c_C64848));
        } else {
            tvSyts.setVisibility(View.GONE);
            tvYxrq.setTextColor(mContext.getResources().getColor(R.color.c_333333));
        }


        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new JgmmxAdapter(this);
        myList.setAdapter(adapter);

        mPresent = new StockDetailPersentr(this, this);
        mPresent.getList(isall, pageindex, pagesize, bean.getBatchnumber(),0);

    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showView(boolean hasData) {

    }

    @Override
    public void showList(ArrayList<StockDetail.ListBean> list) {

        if (list.size() < 10)
            isEnd = false;
        else
            isEnd = true;

        Totle = adapter.getmArrayList().size() + list.size();


        if (type == DataLoadDirection.Refresh) {
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        } else if (type == DataLoadDirection.LoadMore) {
            myList.stopLoadMore();
            pageindex++;
        }

        if (isEnd) {
            myList.showFooterView();
            myList.setPullLoadEnable(true);
        } else {
            myList.hideFooterView();
            myList.setPullLoadEnable(false);
        }

        adapter.setmArrayList(list);
    }

    @Override
    public void showData(StockDetail stockDetail) {
    }

    @Override
    public void toHeaderData() {

    }

    @Override
    public void showCount(int stock) {

    }

    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getList(isall, 0, pagesize, bean.getBatchnumber(),0);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
        type = DataLoadDirection.LoadMore;
        if (isEnd) {
            mPresent.getList(isall, pageindex + 1, pagesize, bean.getBatchnumber(),0);
        }
    }


    @Override
    public void setPresenter(StockDetailContract.Persent presenter) {

    }

    @OnClick(R.id.img_back)
    public void onClick() {
        finish();
    }
}
